package com.zking.frame;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zking.dao.AddressDao;
import com.zking.dao.UserDao;
import com.zking.entity.Address;
import com.zking.entity.User;
import com.zking.utils.Tool工具;

import javax.swing.*;
import javax.swing.text.html.FormSubmitEvent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Map;

public class AddSite extends JDialog {

    // 设置需要的变量
    JLabel jLabel, jLabel1, jLabel2;//存储提示信息
    TextArea textArea;// 存储用户书写的地址信息
    JTextField jTextField, jTextField1;//存储输入的收货人姓名
    JButton jButton; // 设置点击事件
    boolean a=false,b=false,c=false;

    public AddSite(JFrame owner, boolean modal,String name,Address address) {
        super(owner, modal);

        this.setBounds(owner.getX() + 15, owner.getY() + 320, 600, 500);
        this.setResizable(false);// 设置窗口不可调整大小
        this.setTitle(name);
        this.setLayout(null);



        //书写面板
        jLabel = new JLabel("请输入收货地址：");
        jLabel.setBounds(20, 40, 180, 50);
        jLabel.setFont(new Font("黑体", Font.BOLD, 20));
        add(jLabel);

        textArea = new TextArea("请输入详细的地址");
        textArea.setBounds(200, 20, 370, 120);
        textArea.setFont(new Font("黑体", Font.BOLD, 20));
        add(textArea);
        textArea.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent focusEvent) {
                if (textArea.getText().equals("请输入详细的地址")){
                    textArea.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent focusEvent) {
                if (textArea.getText().equals("")&&!textArea.getText().matches("[。]{8,}")){
                    textArea.setText("请输入详细的地址");
                }else{
                    b=true;
                }
            }
        });

        jLabel1 = new JLabel("请输入收货人姓名：");
        jLabel1.setBounds(10, 190, 190, 50);
        jLabel1.setFont(new Font("黑体", Font.BOLD, 20));
        add(jLabel1);

        jTextField = new JTextField();
        jTextField.setBounds(200, 190, 370, 50);
        jTextField.setFont(new Font("黑体", Font.BOLD, 20));
        add(jTextField);

        jTextField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent focusEvent) {
                if (jTextField.getText().equals("收货人不为空")){
                    jTextField.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent focusEvent) {
                if (jTextField.getText().equals("")){
                    jTextField.setText("收货人不为空");
                }
                if (jTextField.getText().equals("收货人不为空")){
                    b=false;
                }
                if (!jTextField.getText().equals("收货人不为空")&&!jTextField.getText().equals("")){
                    b=true;
                }
            }
        });


        jLabel2 = new JLabel("请输入电话号码：");
        jLabel2.setBounds(10, 250, 180, 50);
        jLabel2.setFont(new Font("黑体", Font.BOLD, 20));
        add(jLabel2);

        jTextField1 = new JTextField();
        jTextField1.setBounds(200, 250, 370, 50);
        jTextField1.setFont(new Font("黑体", Font.BOLD, 20));
        add(jTextField1);
        jTextField1.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent focusEvent) {
                if (jTextField1.getText().equals("请输入正确的电话号码")){
                    jTextField1.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent focusEvent) {
                if (!jTextField1.getText().matches("^1[3|4|5|7|8][0-9]\\d{4,8}$")){
                    jTextField1.setText("请输入正确的电话号码");
                }else{
                    c=true;
                }
            }
        });

        jButton = new JButton("确认提交");
        jButton.setBounds(200, 310, 170, 50);
        jButton.setFont(new Font("黑体", Font.BOLD, 20));
        add(jButton);


        //书写点击事件
        //姓名，地址，电话,用户名
        //jLabel1，textArea，jTextField1，username
//        address.getName(), address.getAddress(), address.getPhone(), address.getUsername()
//
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

                if (name.equals("添加地址")){
                if (b&&c) {
                    Address address1 = new Address();
                    address1.setName(jTextField.getText());
                    address1.setAddress(textArea.getText());
                    address1.setPhone(jTextField1.getText());
                    Tool工具 tool工具 = new Tool工具();
                    address1.setUsername(tool工具.getName());
                    AddressDao addressDao = new AddressDao();
                    if(addressDao.insertStudent(address1)==1){
                        JOptionPane.showMessageDialog(AddSite.this, "提交成功", "成功", JOptionPane.OK_CANCEL_OPTION);
                        b=false;c=false;
                        dispose();
                    }else{
                        JOptionPane.showMessageDialog(AddSite.this, "提交失败", "失败", JOptionPane.OK_CANCEL_OPTION);
                    }
                }
            }else{
                 if (b&&c) {
                     Address address1 = new Address();
                     address1.setName(jTextField.getText());
                     address1.setAddress(textArea.getText());
                     address1.setPhone(jTextField1.getText());
                     Tool工具 tool工具 = new Tool工具();
                     address1.setUsername(tool工具.getName());
                     AddressDao addressDao = new AddressDao();
                     if(addressDao.updateAddress(address1,address)==1){

                            JOptionPane.showMessageDialog(AddSite.this, "修改成功", "成功", JOptionPane.OK_CANCEL_OPTION);
                            b=false;c=false;
                            dispose();
                        }else{
                            JOptionPane.showMessageDialog(AddSite.this, "修改失败", "失败", JOptionPane.OK_CANCEL_OPTION);
                        }
                    }
                }
            }
        });
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    }
  // 获取验证码
    public static void getSendSms(String phoneNumber,int random){
        DefaultProfile profile = DefaultProfile.getProfile("default", "<LTAI4G8AEJpuUu69U3ZUakvv>", "<WAwppA0bvPybuyUHsAUyxFWcJPMzVo>");
        //DefaultProfile profile = DefaultProfile.getProfile("default", "", "");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "吃了没验证码");
        request.putQueryParameter("TemplateCode", "SMS_195575807");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+random+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }
}
